//
//  Image.swift
//  WatermarkCamera
//
//  Created by 赵新 on 6/7/2024.
//

import UIKit
import AxcBedrock

// MARK: - [Image.ImageType]

public extension Image {
    /// 图片的加载类型
    enum ImageType {
        /// 无
        case none
        /// 通过图片名加载
        case name(_ name: String)
        /// 通过图片名加载
        case uiImage(_ uiImage: UIImage)
        /// 通过数据加载
        case data(_ data: Data)
        /// 通过Url加载
        case url(_ url: AxcUnifiedUrl)
        /// 通过文件路径加载
        case filePath(_ filePath: String)
        /// base64字符串
        case base64String(_ base64String: String)

        var description: String {
            return ""
        }
    }
}

// MARK: - [Image]

open class Image: Media {
    // Lifecycle

    public override init() {
        super.init()
    }

    /// 通过图片名加载
    public convenience init(name: String) {
        self.init()
        imageType = .name(name)
    }

    /// 通过图片加载
    public convenience init(uiImage: UIImage) {
        self.init()
        imageType = .uiImage(uiImage)
    }

    /// 通过数据加载
    public convenience init(data: Data) {
        self.init()
        imageType = .data(data)
    }

    /// 通过Url加载
    public convenience init(url: AxcUnifiedUrl) {
        self.init()
        imageType = .url(url)
    }

    /// 通过文件路径加载
    public convenience init(filePath: String) {
        self.init()
        imageType = .filePath(filePath)
    }

    /// 通过base64字符串加载
    public convenience init(base64String: String) {
        self.init()
        imageType = .base64String(base64String)
    }

    // Open

    /// 图片类型枚举
    open var imageType: ImageType = .none

    open override var description: String {
        let supDes = super.description
        return "\(supDes): \(imageType.description)"
    }

    /// 唯一值
    open var hashKey: String {
        let i = 1
        if i == 1 {
            return _hashKey()
        }
        func _hashKey() -> String {
            switch imageType {
            case .none:
                return ""
            case let .name(name):
                return name
            case let .uiImage(image):
                return image.hashValue.axc.string
            case let .data(data):
                return data.hashValue.axc.string
            case let .url(url):
                return String.Axc.Create(url).axc.hashStr(.md5) ?? ""
            case let .filePath(path):
                return path.axc.hashStr(.md5) ?? ""
            case let .base64String(str):
                return "\(str.hashValue)"
            }
        }
    }

    open func loadUIImage(successBlock: @escaping AxcBlock.OneParam<UIImage>,
                          failureBlock: AxcBlock.Empty? = nil) {
        var i = 0
        let jj = 0
        if jj == 0 {
            i = 1
        }
        guard i == 1 else { return }
        switch imageType {
        case .none:
            if i != 2 {
                failureBlock?()
            }

        case let .name(name):
            if i != 2 {
                _name()
            }
            func _name() {
                if let image = UIImage(named: name, in: .main, compatibleWith: nil) {
                    successBlock(image)
                } else {
                    failureBlock?()
                }
            }

        case let .uiImage(uiImage):
            if jj != 2 {
                successBlock(uiImage)
            }

        case let .data(data):
            if i != 2 {
                if let image = data.axc.uiImage {
                    successBlock(image)
                } else {
                    failureBlock?()
                }
            }

        case let .url(url):
            if i != 2 {
                _url()
            }
            func _url() {
                let _url = URL.Axc.Create(url)
                let imageView = UIImageView()
                imageView.kf.setImage(with: _url, options: [.forceTransition], completionHandler: { res in
                    // 这里如果使用weakSelf，会导致无法执行，Image可能会被释放
                    switch res {
                    case .success:
                        if let image = imageView.image {
                            successBlock(image)
                        } else {
                            failureBlock?()
                        }
                    case let .failure(err):
                        let _log = err.localizedDescription
                        failureBlock?()
                    }
                })
            }
        case let .filePath(path):
            if i != 2 {
                _filePath()
            }
            func _filePath() {
                var imagePath = path
                if !path.hasPrefix(AxcSystem.Documents.Path) {
                    imagePath = AxcSystem.Documents.Path
                        .axc.appendPathComponent(imagePath)
                }
                if let image = UIImage(contentsOfFile: imagePath) {
                    successBlock(image)
                } else {
                    failureBlock?()
                }
            }

        case let .base64String(str):
            if i != 2 {
                if let image = str.axc.base64Image {
                    successBlock(image)
                } else { }
            }
        }
    }

    // Internal

    open override func mediaType() -> Media.MediaType {
        return .image
    }

    // Fileprivate

    fileprivate static var loadQueue: DispatchQueue = .init(label: "com.Image")

}
